// contact.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    contacts: '',
    pageShow: false,
    appColor: app.appColor
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.showLoadings();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.login();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  login: function () {
    var that = this;
    wx.request({
      url: app.serviceurl + 'Member/GetTakeman',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      dataType: 'json',
      success: function (res) {
        if(!res.data.Result){
          wx.hideLoading();
          return
        }
        if(res.data.TakeManList != null && typeof(res.data.TakeManList) != undefined){
          res.data.TakeManList.forEach(function (item) {
            // item.TakeManPhone = item.TakeManPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            item.TakeManCardIdE = item.TakeManCardId.replace(/(\d{6})\d{4}(\d{4})/, '$1****$2');
          })
        }
        
        that.setData({
          contacts: res.data.TakeManList,
          pageShow: true
        });

        app.hideLoadings();
      },
      fail: function () {
        console.log("调用失败");
        app.hideLoadings();
      }
    })
  },

  addContact: function () {
    wx.navigateTo({
      url: "./addContact/addContact",
    })
  },

  alterUser:function(e){
    let carid = e.currentTarget.dataset.user.TakeManCardId;
    let name = e.currentTarget.dataset.user.TakeName;
    let takemanid = e.currentTarget.dataset.user.TakeManId;
    let TakeManPhone = e.currentTarget.dataset.user.TakeManPhone;
    wx.navigateTo({
      url: `./addContact/addContact?carid=${carid}&name=${name}&takemanid=${takemanid}&phone=${TakeManPhone}`,
    })
  },
  
  openConfirm: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除当前联系人吗?',
      confirmText: "确定",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          // 发起删除请求
          wx.request({
            url: app.serviceurl + 'Member/DeleteTakeman',
            header: {
              Authorization: "Bearer " + app.accesstoken
            },
         data: {
           takemanid: e.currentTarget.dataset.target
            },
            dataType: 'json',
            success: function (res) {
              that.login();
            },
            fail: function (error) {
              console.log(error);
            }
          })
        } 
      }
    });
  }


})